#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <unordered_map>
#include <boost/algorithm/string.hpp> //主要是利用boost里面的split切分字符串
#include "cppjieba/Jieba.hpp"         //利用jieba分词库 cppjieba是当前路径的一个软连接
#include <mutex>
#include "log.hpp"

namespace ns_util
{
    class FileUtil
    {
    public:
        // 工具类，用来提供读取文件的静态方法
        static bool ReadFile(const std::string &file_path, std::string *out)
        {
            std::ifstream in(file_path, std::ios::in);
            if (!in.is_open())
            {
                std::cerr << "open file" << file_path << "error" << std::endl;
                return false;
            }
            std::string line;
            // 如何理解getline读取到文件结束呢？？getline的返回值是一个&，while(bool), 本质是因为重载了bool函数强制类型转化
            while (std::getline(in, line)) // while(bool)
            {
                *out += line;
            }
            in.close(); // 关闭
            return true;
        }
    };

    // 此工具类使用了切分一个个文档doc的
    class StringUtil
    {
    public:
        static void Split(const std::string &target, std::vector<std::string> *out, const std::string &sep)
        {
            // 复用现成的boost::split    不用find....substr
            boost::split(*out, target, boost::is_any_of(sep), boost::token_compress_on); // token...on参数用来将\3\3\3压缩成一个\3
        }
    };

    const char *const DICT_PATH = "./dict/jieba.dict.utf8";
    const char *const HMM_PATH = "./dict/hmm_model.utf8";
    const char *const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char *const IDF_PATH = "./dict/idf.utf8";
    const char *const STOP_WORD_PATH = "./dict/stop_words.utf8";
    // 此工具类，用来进行分词
    class JiebaUtil
    {
    private:
        // static cppjieba::Jieba jieba;
        cppjieba::Jieba jieba;
        std::unordered_map<std::string, bool> stop_words;
        static JiebaUtil *instance;
        static std::mutex mtx;

    private:
        JiebaUtil() : jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH) {}
        JiebaUtil(const JiebaUtil &) = delete;
        JiebaUtil &operator=(const JiebaUtil &) = delete;

    public:
        static JiebaUtil *GetInstance()
        {
            if (instance == nullptr)
            {
                mtx.lock();
                if (instance == nullptr)
                {
                    instance = new JiebaUtil();
                    instance->InInitJiebaUtil();
                }
                mtx.unlock();
            }
            return instance;
        }

    public:
        void InInitJiebaUtil()
        {
            std::ifstream in(STOP_WORD_PATH);
            if (!in.is_open())
            {
                LOG(FATAL, "load stop words file failed");
                return;
            }
            std::string line;
            while (std::getline(in, line))
            {
                stop_words.insert({line, true});
            }
            in.close();
        }
        void CutStringHelper(const std::string &src, std::vector<std::string> *out)
        {
            jieba.CutForSearch(src, *out);
            for (auto iter = out->begin(); iter != out->end();)
            {
                // 根据暂停词本stop_words对*out里面去掉暂停词
                if (stop_words.find(*iter) != stop_words.end())
                {
                    iter = out->erase(iter);
                }
                else
                {
                    iter++;
                }
            }
        }

    public:
        static void CutString(const std::string &src, std::vector<std::string> *out)
        {
            // jieba.CutForSearch(src, *out);
            ns_util::JiebaUtil::GetInstance()->CutStringHelper(src, out);
        }
    };
    // cppjieba::Jieba JiebaUtil::jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);
    JiebaUtil *JiebaUtil::instance = nullptr;
    std::mutex JiebaUtil::mtx;
}